#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

try:
    import wx
except ImportError:
    raise ImportError,"The wxPython module is required to run this program"

try:
    import os
except ImportError:
    raise ImportError,"The OS module is required to run this program"

try:
    import socket, struct, fcntl
except ImportError:
    raise ImportError,"Socket module is required to run this program"

try:
    from threading import Thread
except ImportError:
    raise ImportError,"Threading module is required to run this program"

try:
    import time
except ImportError:
    raise ImportError,"time module is required to run this program"




class TCPDPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent, page):
        """"""
        wx.Panel.__init__(self, parent=parent)
        self.page = page	
	font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(9)        
	
	self.s = socket.socket()
	self.ss = socket.socket()
	self.s1 =0
	self.s2 =0
	hsizer = wx.BoxSizer(wx.HORIZONTAL)		
	hsizer1= wx.BoxSizer(wx.HORIZONTAL)
 	hsizer2= wx.BoxSizer(wx.HORIZONTAL)
	hsizer3= wx.BoxSizer(wx.HORIZONTAL)
	hsizer4= wx.BoxSizer(wx.HORIZONTAL)
	hsizer5= wx.BoxSizer(wx.HORIZONTAL)

	sb1 = wx.StaticBox(self, label="Trazador de ruta")             
	
	self.tc0 = wx.TextCtrl(self, style=wx.TE_READONLY | wx.TE_MULTILINE | wx.HSCROLL)
	self.tc0.SetFont(font)
	wx.EVT_TEXT(self.tc0, self.tc0.GetId(), self.OnText) 
        
	self.st1 = wx.StaticText(self, label='IP destino:')
        self.tc1 = wx.TextCtrl(self)

 	self.btn1 = wx.Button(self, label='Trazar', size=(100, 26))
        self.btn1.SetFont(font)
 	self.btn1.Bind(wx.EVT_BUTTON, self.onTar)
        
	hsizer1.Add(self.st1, flag=wx.RIGHT | wx.BOTTOM , border=8)
        hsizer1.Add(self.tc1, proportion=1)
	hsizer1.Add(self.btn1, flag=wx.RIGHT, border=0)
	
	boxsizer1 = wx.StaticBoxSizer(sb1, wx.VERTICAL)
	boxsizer1.Add(self.tc0, proportion=1, flag=wx.EXPAND) 
	boxsizer1.Add(hsizer1, flag=wx.RIGHT | wx.EXPAND, border=0) 

        sb2 = wx.StaticBox(self, label="Aplicación de prueba:ECHO")             
	
	self.tc2 = wx.TextCtrl(self, style=wx.TE_READONLY | wx.TE_MULTILINE | wx.HSCROLL)
	self.tc2.SetFont(font)
	wx.EVT_TEXT(self.tc2, self.tc2.GetId(), self.OnText) 
        
	self.st2 = wx.StaticText(self, label='Nombre?')

        self.tc3 = wx.TextCtrl(self)

 	self.btn2 = wx.Button(self, label='Enviar', size=(80, 26))
        self.btn2.SetFont(font)
 	self.btn2.Bind(wx.EVT_BUTTON, self.onECHO)
	
	self.btn3 = wx.Button(self, label='Detener', size=(80, 26))
        self.btn3.SetFont(font)
 	self.btn3.Bind(wx.EVT_BUTTON, self.onSTOP)
	self.btn3.Disable();
        
	hsizer2.Add(self.st2, flag=wx.RIGHT | wx.BOTTOM , border=8)
        hsizer2.Add(self.tc3, proportion=1)
	hsizer2.Add(self.btn2, flag=wx.RIGHT, border=0)
	hsizer2.Add(self.btn3, flag=wx.RIGHT, border=0)
	
	boxsizer2 = wx.StaticBoxSizer(sb2, wx.VERTICAL)
	boxsizer2.Add(self.tc2, proportion=1, flag=wx.EXPAND) 
	boxsizer2.Add(hsizer2, flag=wx.RIGHT | wx.EXPAND, border=0) 

	sb3 = wx.StaticBox(self, label="Aplicación de prueba:STREAM")             
	
	self.tc4 = wx.TextCtrl(self, style=wx.TE_READONLY | wx.TE_MULTILINE | wx.HSCROLL)
	self.tc4.SetFont(font)
	wx.EVT_TEXT(self.tc4, self.tc4.GetId(), self.OnText) 
        
	self.st3 = wx.StaticText(self, label='Seg/linea')

        self.tc5 = wx.TextCtrl(self)

 	self.btn4 = wx.Button(self, label='Enviar', size=(80, 26))
        self.btn4.SetFont(font)
 	self.btn4.Bind(wx.EVT_BUTTON, self.onSTREAM)
	
	self.btn5 = wx.Button(self, label='Detener', size=(80, 26))
        self.btn5.SetFont(font)
 	self.btn5.Bind(wx.EVT_BUTTON, self.onSSTOP)
	self.btn5.Disable();
        
	hsizer3.Add(self.st3, flag=wx.RIGHT | wx.BOTTOM , border=8)
        hsizer3.Add(self.tc5, proportion=1)
	hsizer3.Add(self.btn4, flag=wx.RIGHT, border=0)
	hsizer3.Add(self.btn5, flag=wx.RIGHT, border=0)
	
	boxsizer3 = wx.StaticBoxSizer(sb3, wx.VERTICAL)
	boxsizer3.Add(self.tc4, proportion=1, flag=wx.EXPAND) 
	boxsizer3.Add(hsizer3, flag=wx.RIGHT | wx.EXPAND, border=0) 



	
	hsizer.Add(boxsizer1,proportion=1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
	hsizer.Add(boxsizer2,proportion=1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)	
	hsizer.Add(boxsizer3,proportion=1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)		
	self.SetSizer(hsizer)
        
	self.Show(True)

    def OnText(self, event): 
        self.tc0.ScrollPages(1) 

    def onTar(self, event):
	lines=[]	
	lines.append(os.popen('traceroute '+str(self.tc1.GetValue())).readline())					
	for l in lines:        
		self.tc0.AppendText(l)

    def onECHO(self, event):
	self.btn2.Disable()
	self.btn3.Enable()
	self.s1=1
	Thread(target=self.Onrecho).start()

    def onSTOP(self, event):
	self.btn2.Enable()
	self.btn3.Disable()
	self.s1=0
	self.s.close()

    def onSTREAM(self, event):
	self.btn4.Disable()
	self.btn5.Enable()
	self.s2=1
	Thread(target=self.Onstream).start()

    def onSSTOP(self, event):
	self.btn4.Enable()
	self.btn5.Disable()
	self.s2=0
	self.ss.close()
    
    def Updatet2(self, msg):
	self.tc2.AppendText(msg)

    def Updatet4(self, msg):
	self.tc4.AppendText(msg)  

    def Onrecho(self):
	name=str(self.tc3.GetValue())+' '	
	self.s.connect((str(self.tc1.GetValue()),3000))
	self.s.sendall(name)
		

	while (self.s1==1):
           data = self.s.recv(64)
    	   if not data: break
    	   wx.CallAfter(self.Updatet2, data)
    
    def Onstream(self):
    	time=str(self.tc5.GetValue())	
    	self.ss.connect((str(self.tc1.GetValue()),5000))
    	self.ss.sendall(time)

    	while (self.s2==1):
           data = self.ss.recv(64)
           if not data: break
           wx.CallAfter(self.Updatet4, data)
    
	

class simpleapp_wx(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,wx.ID_ANY,
                          "Projecto <-> Quimera (Demostraciones)",size=(1200,400))
        self.initialize()


    def initialize(self):
       
 	panel = wx.Panel(self)
        notebook = wx.Notebook(panel)
        tcpdtab = TCPDPanel(notebook, 1)        
	notebook.AddPage(tcpdtab, "Aplicaciones de prueba")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()
        self.Show()


if __name__ == "__main__":
    app = wx.App()
    frame = simpleapp_wx()
    app.MainLoop()
